#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

char dst_title1[] = "resultimage1";
char dst_title2[] = "resultimage2";
Mat src, dst1, dst2;
int element_size = 3;
int max_size = 21;
void CallBack_Demo(int, void*);
void CallBack_Demo2(int, void*);

int main() {
  src = imread("../images/2.jpg");
  if (src.empty()) {
    cout << "could not load image.." << endl;
    return -1;
  }
  namedWindow("opencv setup demo", CV_WINDOW_AUTOSIZE);
  imshow("opencv setup demo", src);

  //   erode(src, dst2, kernel);
  //   imshow(dst_title2, dst2);

  namedWindow(dst_title1, CV_WINDOW_AUTOSIZE);
  createTrackbar("Element size:", dst_title1, &element_size, max_size,
                 CallBack_Demo);
  CallBack_Demo(0, 0);
  namedWindow(dst_title2, CV_WINDOW_AUTOSIZE);
  createTrackbar("Element size:", dst_title2, &element_size, max_size,
                 CallBack_Demo2);
  CallBack_Demo2(0, 0);

  waitKey(0);
  return 0;
}

void CallBack_Demo(int, void*) {
  int s = element_size * 2 + 1;
  Mat kernel = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1));
  dilate(src, dst1, kernel, Point(-1, -1));
  imshow(dst_title1, dst1);
}

void CallBack_Demo2(int, void*) {
  int s = element_size * 2 + 1;
  Mat kernel = getStructuringElement(MORPH_RECT, Size(s, s), Point(-1, -1));
  erode(src, dst2, kernel, Point(-1, -1));
  imshow(dst_title2, dst2);
}
